package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;

@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Override
    public void batchOrderSetting(List<String[]> list) {
        List<OrderSetting> orderSettings = StringToList(list);
        List<OrderSetting> orderSettingSave = new ArrayList<>();
        List<OrderSetting> orderSettingUpdate = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ORDERDATE", orderDate);

            OrderSetting orderSettingIsExist = baseMapper.selectOne(queryWrapper);
            if (orderSettingIsExist != null) {
                orderSetting.setId(orderSettingIsExist.getId());
                orderSettingUpdate.add(orderSetting);
            } else {
                orderSettingSave.add(orderSetting);
            }
        }
        saveBatch(orderSettingSave);
        updateBatchById(orderSettingUpdate);
    }

    @Override
    public Map findSettingData(int year, int month) {
        String beginDate = year + "-" + month + "-1";
        String endDate = year + "-" + month + "-31";


        List<OrderSetting> list = baseMapper.findSettingData(beginDate,endDate);

        Map map = new HashMap<>();
        for (OrderSetting orderSetting : list) {
            Date key = orderSetting.getOrderDate();
            String s = DateUtils.parseDate2String(key, "yyyy-MM-dd");

            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            OrderSetting orderSetting1 = new OrderSetting();
            orderSetting1.setReservations(reservations);
            orderSetting1.setNumber(number);
            map.put(s,orderSetting1);
        }
        return map;
    }

    @Override
    public void updateNumberByOrderDate(String date, int number) {
        baseMapper.updateNumberByOrderDate(date,number);
    }

    @Override
    public int isOrderSettingOK(String orderDate) {
        return baseMapper.isOrderSettingOK(orderDate);
    }

    @Override
    public void updateResetvationSByOrderDate(String orderDate) {
        baseMapper.updateResetvationSByOrderDate(orderDate);
    }

    private List<OrderSetting> StringToList(List<String[]> list) {
        if (list != null && list.size() != 0) {
            List<OrderSetting> orderSettingsList = new ArrayList<>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setReservations(0);
                orderSettingsList.add(orderSetting);
            }
            return orderSettingsList;
        } else {
            return null;
        }
    }
}
